package cn.test.livedatademo

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData

class FirstFragment() : Fragment() {
    private lateinit var contentTv: TextView
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_first, container, false)
        contentTv = view.findViewById(R.id.contentTv)
        observeData()
        return view
    }

    private fun observeData() {
        (activity as MainActivity).nameLiveData.observe(requireActivity()) {
            contentTv.text = it
        }
    }
}